// pages/TradingArea/GiftBagDetail2.js
const controller=require("../controllers/Controller").controller

const app=getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponBagId:"",
    detail:"",
    shopDetail:"",
    shopkey:""
  },

  //礼券包详情 2.0
  getBagActivityDetail(){
    controller.getActivityDetail({couponBagId:this.data.couponBagId}).then(res=>{
      this.setData({
        detail:res.data
      })
      this.setData({
        shopkey:Object.keys(res.data.couponMap)[0]
      })
      this.setData({
        shopDetail:res.data.couponMap[this.data.shopkey]
      })
    })
  },

  //获取礼包轮播图
  getSwiperImg(){
    controller.getSwiperImg({shopGroupId:this.data.shopGroupId}).then(res=>{
      this.setData({
        imgList:res.data
      })
    })
  },

  toShopDetail(e){
    let shopId=e.currentTarget.dataset.shopid
    wx.navigateTo({
      url: '/pages/Index/Index?shopId='+shopId,
    })
  },


  //生成订单购买
  createCouponOrder(){
    let value={}
    value.couponBagId=this.data.couponBagId
    if(this.data.invitationAccountId){
      value.invitationAccountId=this.data.invitationAccountId
    }
    controller.createCouponOrder(value).then(res=>{
      let data=res.data
      this.pay(data)
    })
  },

  pay(data){
    let value={}
    value.openId=app.globalData.openId
    value.orderNo=data.orderNo
    value.type="GC"
    controller.weChatPay(value).then(res=>{
      let data=JSON.parse(res.data)
      wx.requestPayment({
        nonceStr: data.nonceStr,
        package: data.package,
        paySign: data.paySign,
        timeStamp: data.timeStamp,
        signType: data.signType,
        success(res){
          wx.showToast({
            title: "支付成功",
            icon: 'none',
            duration: 1000
          });
          setTimeout(function(){
            wx.navigateBack()
          },1500)
        },
        fail(res){
          wx.showToast({
            title: "支付失败",
            icon: 'none',
            duration: 1000
          });
        }
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      couponBagId:options.id,
      shopGroupId:options.shopGroupId
    })
    if(options.invitationAccountId){
      this.setData({
        invitationAccountId:options.invitationAccountId
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getBagActivityDetail()
    this.getSwiperImg()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let couponBagId=this.data.couponBagId
    let invitationAccountId=wx.getStorageSync('shopId')
    let shopGroupId=this.data.shopGroupId
    let title=this.data.detail.shopGroupName

    return {
      title:title,
      url:"/pages/TradingArea/GiftBagDetail2?id="+couponBagId+"&invitationAccountId="+invitationAccountId+"&shopGroupId="+shopGroupId
    }
  },
  //跳转优惠券详情
  toCouponDetail(){
    let id=e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/TradingArea/couponDetail?id='+id,
    })
  }
})